# reference: 20190519_随机森林.ipynb
import pandas as pd
import numpy as np
import re, os, sys

path = "./data/playball.txt"
data_df = pd.read_csv(path, sep=" ", index_col='Day')
y_label = 'PlayTennis'
dim_list = [i for i in data_df.columns if i not in  [y_label]]

from sklearn.preprocessing import LabelEncoder
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score
from sklearn.metrics import f1_score

clf = RandomForestClassifier(n_estimators=10)
label_Code = LabelEncoder()
data_copy_df = data_df.copy()
for feature in data_copy_df.columns:
    data_copy_df[feature] = label_Code.fit_transform(data_copy_df[feature])

x_train, x_test, y_train, y_test = train_test_split(data_copy_df[dim_list], data_copy_df.PlayTennis,test_size=0.33)
clf.fit(x_train, y_train)
clf.score(x_test, y_test)

pred_x = clf.predict(x_test)
precision_score(pred_x, y_test.values)
score1 = recall_score(pred_x, y_test.values)
score2 = f1_score(pred_x, y_test.values)

print(score1)
print(score2)